<?php
namespace app\admin\controller;

use app\common\model\RebateModel;
use think\Log;
use app\common\model\RewardModel;
use app\common\model\MemberModel;
use common\util\ArrayUtil;

/**
 * 返利
 *
 * @author jinxiwang
 *        
 */
class Reward extends Base {
    

    /*
     * 初始化操作
     */
    public function _initialize() {
        $this->needLogin();
    }
    
    /**
     * 订单返利
     */
    public function order_reward(){
        $mid = $this->request->param('mid',0);
        $this->assign('mid',$mid);
        return $this->fetch();
    }
    
    public function  order_reward_rs(){
        
        $size = 10;

        $status = $this->request->param('status', '');
        $start = $this->request->param('start',0);
        $startDate = $this->request->param('start_date', '');
        $endDate = $this->request->param('end_date', '');
        $keywords = $this->request->param('keywords', '');
        $draw = $this->request->param('draw', '1', 'intval');
        $mid = $this->request->param('mid', '0', 'intval');
        
        $type = 1;
        $where = [];
        $where['type'] = $type;
        if($startDate){
            $startDate = date('Ymd',strtotime($startDate));
        }
        if($endDate){
            $endDate = date('Ymd',strtotime($endDate));
        }
        if($startDate && $endDate){
            $where['date'] = ['between',[$startDate,$endDate]];
        }else if($startDate){
            $where['date'] = ['>=',$startDate];
        }else if($endDate){
            $where['date'] = ['<=',$startDate];
        }
        
        if($keywords){
            $mids = MemberModel::instance()->searchMidsByKeywords($keywords);
            $where['mid'] = ['in',$mids];
        }
        
        if($status != ''){
            $where['status'] = $status;
        }
        if($mid){
            $where['mid'] = $mid;
        }
        
        $items = RewardModel::instance()->where($where)->order('id','desc')->limit($start,$size)->select();
        $items = collection($items)->toArray();
        $mids = ArrayUtil::getKeyValuesFromArray($items, 'mid');
        $midMap = [];
        if($mids){
            $midMap = MemberModel::instance()->whereIn('id',$mids)->column('name','id');
        }
        $count= RewardModel::instance()->where($where)->count();
        $list = [];
        foreach ($items as $item){
            $mid = $item['mid'];
            $name = array_key_exists($mid, $midMap) ? $midMap[$mid] : '';
            $item['name'] = $name;
            $item['cdate'] = date('Y-m-d H:i:s',$item['ctime']);
            $item['settlement_date'] = date('Y-m-d H:i:s',$item['settlement_time']);
            $list [] = $item;
        }
        
        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
    }
    
    /**
     * 
     */
    public function month_reward(){
        return $this->fetch();
    }
    
    public function month_reward_rs(){
        
        $size = 10;
        
        $status = $this->request->param('status', '');
        $start = $this->request->param('start',0);
        $startDate = $this->request->param('start_date', '');
        $endDate = $this->request->param('end_date', '');
        $keywords = $this->request->param('keywords', '');
        $draw = $this->request->param('draw', '1', 'intval');
        
        $type = 2;
        $where = [];
        $where['type'] = $type;
        if($startDate){
            $startDate = date('Ym',strtotime($startDate));
        }
        if($endDate){
            $endDate = date('Ym',strtotime($endDate));
        }
        if($startDate && $endDate){
            $where['month'] = ['between',[$startDate,$endDate]];
        }else if($startDate){
            $where['month'] = ['>=',$startDate];
        }else if($endDate){
            $where['month'] = ['<=',$startDate];
        }
        
        if($keywords){
            $mids = MemberModel::instance()->searchMidsByKeywords($keywords);
            $where['mid'] = ['in',$mids];
        }
        
        if($status != ''){
            $where['status'] = $status;
        }
        
        $count= RewardModel::instance()->where($where)->count();
        
        $items = RewardModel::instance()->where($where)->order('id','desc')->limit($start,$size)->select();
        $items = collection($items)->toArray();
        $mids = ArrayUtil::getKeyValuesFromArray($items, 'mid');
        $midMap = [];
        if($mids){
            $midMap = MemberModel::instance()->whereIn('id',$mids)->column('name','id');
        }
        
        $list = [];
        foreach ($items as $item){
            $mid = $item['mid'];
            $name = array_key_exists($mid, $midMap) ? $midMap[$mid] : '';
            $item['name'] = $name;
            $item['cdate'] = date('Y-m-d H:i:s',$item['ctime']);
            $item['settlement_date'] = date('Y-m-d H:i:s',$item['settlement_time']);
            $list [] = $item;
        }
        
        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
    }
    
    public function leader_reward(){
        return $this->fetch();
    }

    public function leader_reward_rs(){
        
        $size = 10;
        
        $status = $this->request->param('status', '');
        $start = $this->request->param('start',0);
        $startDate = $this->request->param('start_date', '');
        $endDate = $this->request->param('end_date', '');
        $keywords = $this->request->param('keywords', '');
        $draw = $this->request->param('draw', '1', 'intval');
        
        $type = 3;
        $where = [];
        $where['type'] = $type;
        if($startDate){
            $startDate = date('Ym',strtotime($startDate));
        }
        if($endDate){
            $endDate = date('Ym',strtotime($endDate));
        }
        if($startDate && $endDate){
            $where['month'] = ['between',[$startDate,$endDate]];
        }else if($startDate){
            $where['month'] = ['>=',$startDate];
        }else if($endDate){
            $where['month'] = ['<=',$startDate];
        }
        
        if($keywords){
            $mids = MemberModel::instance()->searchMidsByKeywords($keywords);
            $where['mid'] = ['in',$mids];
        }
        
        if($status != ''){
            $where['status'] = $status;
        }
        
        $count= RewardModel::instance()->where($where)->count();
        
        $items = RewardModel::instance()->where($where)->order('id','desc')->limit($start,$size)->select();
        $items = collection($items)->toArray();
        $mids = ArrayUtil::getKeyValuesFromArray($items, 'mid');
        $midMap = [];
        if($mids){
            $midMap = MemberModel::instance()->whereIn('id',$mids)->column('name','id');
        }
        
        $list = [];
        foreach ($items as $item){
            $mid = $item['mid'];
            $name = array_key_exists($mid, $midMap) ? $midMap[$mid] : '';
            $item['name'] = $name;
            $item['cdate'] = date('Y-m-d H:i:s',$item['ctime']);
            $item['settlement_date'] = date('Y-m-d H:i:s',$item['settlement_time']);
            $list [] = $item;
        }
        
        $data = array(
            "returnCode" => 1,
            "returnMsg" => "success!",
            'recordsTotal' => $count,
            'recordsFiltered' => $count,
            'draw' => $draw,
            'data' => $list
        );
        return json($data);
        
    }
}
